package com.dirven.game.sprite;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.dirven.game.Coord;

public class Sprite extends com.badlogic.gdx.graphics.g2d.Sprite {
	
	public final static int TYPE_NPC = 0,
							TYPE_OBJECT = 1,
							TYPE_BACKGROUND = 2;
	
	public final static int DIRECTION_NORTH = 0,
							DIRECTION_SOUTH = 1,
							DIRECTION_EAST = 2,
							DIRECTION_WEST = 3;
	
	private int type;
	
	private String folder;
	
	private Coord coord;
	
	private boolean despawn = false;
	
	private ArrayList<JointSprite> joints;
	
	public Sprite(float x, float y, int type, String folder, String image) {
		super(new Texture(Gdx.files.internal("data/images/sprites/" + folder + "/" + image)));
		
		super.setPosition(x, y);
		
		this.type = type;
		this.folder = folder;
		
		coord = new Coord(x, y);
		
		joints = new ArrayList<JointSprite>();
	}
	
	/**
	 * Set the sprite's image
	 * 
	 * @param image
	 */
	public void setImage(String image) {
		super.setTexture(new Texture(Gdx.files.internal("data/images/sprites/" + folder + "/" + image)));
	}
	
	public Coord getPosition() {
		return coord;
	}
	
	/**
	 * Shift the sprite's position.
	 * @param dx
	 * @param dy
	 */
	public void shiftPosition(float dx, float dy) {
		float x = super.getX();
		float y = super.getY();
		
		x += dx;
		y += dy;
		
		super.setPosition(x, y);
		coord.x = x;
		coord.y = y;
	}
	
	/**
	 * Set the x position of the sprite.
	 * 
	 * @param x
	 */
	public void setX(float x) {
		super.setPosition(x, super.getY());
		coord.x = x;
	}
	
	/**
	 * Set the y position of the sprite.
	 * 
	 * @param y
	 */
	public void setY(float y) {
		super.setPosition(super.getX(), y);
		coord.y = y;
	}
	
	/**
	 * Rotate the sprite clockwise
	 * @param degrees
	 */
	public void rotateCW(float degrees) {
		super.rotate(degrees);
	}
	
	/**
	 * Rotate the sprite counter clock wise
	 * 
	 * @param degrees
	 */
	public void rotateCCW(float degrees) {
		super.rotate(-degrees);
	}
	
	/**
	 * Called every time the screen updates
	 * 
	 */
	public void update() {
		updateJointSprites();
	}
	
	public boolean isDespawning() {
		return despawn;
	}
	
	public void despawn() {
		despawn = true;
	}
	
	public void setDespawning(boolean val) {
		despawn = val;
	}
	
	/**
	 * Resets the rotation of the sprite to the original rotation.
	 */
	public void resetRotation() {
		super.rotate(-super.getRotation());
	}
	
	/**
	 * Adds a sprite that is attatched to this sprite.
	 * @param jointSprite
	 */
	public void addJointSprite(Sprite jointSprite, float xOffset, float yOffset) {
		joints.add(new JointSprite(jointSprite, xOffset, yOffset));
	}
	
	/**
	 * Adds a sprite that is attatched to this sprite.
	 * @param jointSprite
	 */
	public void addJointSprite(Sprite jointSprite, int jointLocation) {
		joints.add(new JointSprite(jointSprite, this, jointLocation));
	}
	
	private void updateJointSprites() {
		for(JointSprite js : joints) {
			Sprite joint = js.joint;
			joint.setPosition(this.getX() + js.xOffset, this.getY() + js.yOffset);
			joint.resetRotation();
			joint.setRotation( super.getRotation() );
			joint.update();
		}
	}
	
	public void finalize() {
		// Dispose of texture
		if(super.getTexture() != null) {
			super.getTexture().dispose();
		}
		
		try {
			super.finalize();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
